using System;
using Microsoft.SharePoint.Utilities;

namespace WoltersKluwer.SPChainGang.Core.ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmpty(this string source)
        {
            return string.IsNullOrEmpty(source);
        }

        public static string Append(this string source, string value)
        {
            return string.Concat(source, value);
        }

        public static string Append(this string source, params string[] values)
        {
            var array = new string[values.Length + 1];

            array.SetValue(source, 0);
            Array.Copy(values, 0, array, 1, values.Length);

            return string.Concat(array);
        }

        public static string AppendBefore(this string source, string value)
        {
            return string.Concat(value, source);
        }

        public static string AppendBefore(this string source, params string[] values)
        {
            var array = new string[values.Length + 1];

            Array.Copy(values, array, values.Length);
            array.SetValue(source, array.Length - 1);

            return string.Concat(array);
        }

        public static int ConvertToInt(this string source)
        {
            return int.Parse(source);
        }

        public static bool EqualsByContent(this string source,string compareTo)
        {
            return String.Compare(source, compareTo, StringComparison.OrdinalIgnoreCase) == 0;
        }

        public static string HtmlEscaped(this string source)
        {
            return source != null ? SPEncode.HtmlEncode(source) : String.Empty;
        }

        public static Guid ConvertToGuid(this string source)
        {
            if (!source.IsNullOrEmpty())
            {
                try
                {
                    return new Guid(source);
                }
                catch (ArgumentNullException) { }
                catch (FormatException) { }
                catch (OverflowException) { }
            }

            return Guid.Empty;
        }

        public static Uri GetAbsoluteUri(this string link, Uri baseUri)
        {
            if (link.IsNullOrEmpty())
                return null;

            if (link.StartsWith("www.") && (baseUri.Scheme == Uri.UriSchemeHttp || baseUri.Scheme == Uri.UriSchemeHttps))
            {
                //absolute, but the protocol omitted
                link = String.Format("{0}://{1}", baseUri.Scheme, link);
            }

            Uri linkUri;
            //to prevent double escaping if already (partially) escaped
            var linkEscaped = Uri.EscapeUriString(Uri.UnescapeDataString(link));

            //links containing anchors are not validated neither as relative nor absolute url
            var linkForCheck = linkEscaped;
            if (linkEscaped.Contains("#"))
            {
                linkForCheck = linkEscaped.Remove(linkEscaped.IndexOf("#"));
            }

            if (Uri.IsWellFormedUriString(linkForCheck, UriKind.Relative))
            {
                linkUri = new Uri(baseUri, linkEscaped);
            }
            else
            {
                if (Uri.IsWellFormedUriString(linkForCheck, UriKind.Absolute))
                {
                    linkUri = new Uri(linkEscaped);
                }
                else
                {
                    return null; //Incorrect Link
                }
            }
            return linkUri;
        }
    }
}